package com.mc.lp.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.mc.lp.database.dao.IMicroCampusEntityDAO;
import com.mc.lp.database.entity.AcademicRecordEntity;
import com.mc.lp.database.entity.GradeYearMapperEntity;
import com.mc.lp.database.entity.MicroCampusEntity;
import com.mc.lp.database.entity.helper.MicroCampusHelper;
import com.mc.lp.database.entity.type.SchoolGrade;
import com.mc.lp.database.exception.ConcurrentAccessException;
import com.mc.lp.database.service.handler.constant.SearchParam;
import com.mc.lp.service.AcademicRecordService;
import com.mc.lp.service.MicroCampusAbstractService;
import com.mc.lp.service.MicroCampusEntityService;
import com.mc.lp.shared.vo.KeyValue;
import com.mc.lp.shared.vo.StudentProfileVO;
/*
 * @author aphanda
 * @date Dec 24, 2014
 * @time 3:07:10 PM
 */

@Service
public class MicroCampusEntityServiceImpl extends
MicroCampusAbstractService<MicroCampusEntityServiceImpl> implements MicroCampusEntityService{

	@Resource
	private IMicroCampusEntityDAO microCampusEntityDAO;
	
	@Resource
	private AcademicRecordService academicRecordService;

	
	@Transactional
	public List<StudentProfileVO> getChildFromParentId(Long parentId) {
		List<StudentProfileVO> studentProfileList = new ArrayList<StudentProfileVO>();
		
		Set<MicroCampusEntity> list = microCampusEntityDAO.getChildsByParentId(parentId);
		MicroCampusHelper helper = new MicroCampusHelper();

		for (MicroCampusEntity microCampusEntity : list) {
			StudentProfileVO profile = helper.getVO(microCampusEntity);
			Set<KeyValue> totalSubjects = new HashSet<KeyValue>();
			Set<KeyValue> totalGradeStudied = new HashSet<KeyValue>();

			Set<GradeYearMapperEntity> gradeYearMapper = microCampusEntity.getGradeYearMapper();
			List<AcademicRecordEntity> acads = academicRecordService.getAllAcademicsByStudentEntity(profile.getEntityId());

			for (GradeYearMapperEntity gradeYearMapperEntity : gradeYearMapper) {
				for (AcademicRecordEntity record : acads) {
					if (gradeYearMapperEntity.getSchoolGrade() == (record.getGradeId())) {
						totalSubjects.add(new KeyValue(record.getSubject().getId().toString(), record.getSubject().getSubjectName()));
						totalGradeStudied.add(new KeyValue(String.valueOf(record.getGradeId()), SchoolGrade.getSchoolGradeName(record.getGradeId())));
					}
				}
			}

			profile.setSubjectList(new ArrayList<KeyValue>(totalSubjects));
			profile.setTotalGradeInfo(new ArrayList<KeyValue>(totalGradeStudied));
			studentProfileList.add(profile);
		}
		
		return studentProfileList;
	}

	@Transactional
	public MicroCampusEntity saveMicroCampusEntity(MicroCampusEntity entity) {
		MicroCampusEntity en = null;
		try {
			en =  microCampusEntityDAO.saveOrUpdate(entity);
		} catch (DataAccessException e) {
			e.printStackTrace();
		} catch (ConcurrentAccessException e) {
			e.printStackTrace();
		}
		return en;
	}
	
	@Transactional
	@Override
	public MicroCampusEntity getEntityById(Long id) {
		try{
			return microCampusEntityDAO.get(id);
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return null;
		
	}

	@Transactional
	public Set<StudentProfileVO> getEntitiesBySearchParam(SearchParam searchParam, String searchValue){
		Set<StudentProfileVO> studentProfiles = new HashSet<StudentProfileVO>();
		
		Set<MicroCampusEntity> stuList = new HashSet<MicroCampusEntity>();
		
		if(searchParam.equals(SearchParam.USER_ID)){
			stuList =  microCampusEntityDAO.getChildsByParentId(NumberUtils.toLong(searchValue));
		}else{
			stuList = microCampusEntityDAO.getEntitiesBySearchParam(searchParam, searchValue);
			
		}
		
		MicroCampusHelper helper = new  MicroCampusHelper();
		for (MicroCampusEntity microCampusEntity : stuList) {
			StudentProfileVO vo = helper.getVO(microCampusEntity);
			studentProfiles.add(vo);
		}
		
		return studentProfiles;
	}

	@Override
	@Transactional
	public StudentProfileVO getStudentProfileById(Long id) {
		try{
			MicroCampusEntity studentEntity = microCampusEntityDAO.get(id);
			MicroCampusHelper helper  = new MicroCampusHelper() ; 
			StudentProfileVO profile = helper.getVO(studentEntity);
			return profile;
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return null;
		
	}
	
}
